package 剑指Offer.medium;

import java.util.Arrays;

public class 重建二叉树 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    class Solution {
        public TreeNode buildTree(int[] preorder, int[] inorder) {
            //递归终止条件
            if (preorder.length == 0 && inorder.length == 0) {
                return null;
            }
            //子树的根节点
            TreeNode root = new TreeNode(preorder[0]);
            //左右子树划分
            for (int i = 0; i < preorder.length; i++) {
                if (inorder[i] == root.val) {
                    int[] pre_left = Arrays.copyOfRange(preorder, 1, i + 1);
                    int[] pre_right = Arrays.copyOfRange(preorder, i + 1, preorder.length);
                    int[] in_left = Arrays.copyOfRange(inorder, 0, i);
                    int[] in_right = Arrays.copyOfRange(inorder, i + 1, inorder.length);
                    root.left = buildTree(pre_left, in_left);
                    root.right = buildTree(pre_right, in_right);
                    break;
                }
            }
            return root;
        }
    }
}
